package com.wlps.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlps.bo.OrderBO;
import com.wlps.bo.OrderInfo;
import com.wlps.bo.QueryComposeBo;
import com.wlps.entity.Client;
import com.wlps.entity.Commodity;
import com.wlps.entity.MissionList;
import com.wlps.entity.Order;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.IClientService;
import com.wlps.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *      订单增删查改Controller，
 * </p>
 * @author winner
 * @date 2020/8/3
 */
@Api(tags="订单管理模块")
@RestController
@RequestMapping("/api/order_manager/")
@Valid
public class OrderManageController {
    @Resource
    private OrderCenter orderCenter;
//    @Resource
//    private IClientService clientCenter;
    @GetMapping("query_all_order/{orderStatus}")
    @ApiOperation("查询所有订单 0普通 1撤销 2换货 3退货 4全部")
    public ResultVo<List<Order>> queryAllOrder(@PathVariable @ApiParam("查询订单状态")int orderStatus){
        List<Order> orders = orderCenter.queryOrder(orderStatus);
        return ResultUtils.success("查询成功",orders);
    }
    @PostMapping("query_order_on_require")
    @ApiOperation("按条件查询Order")
    public ResultVo<List<Order>>queryOrderOnRequire(@RequestBody QueryComposeBo queryOrderBo){
        List<Order> orders = orderCenter.queryOrdersCompose(queryOrderBo);
        return ResultUtils.success("查询成功",orders);
    }
    @GetMapping("query_sysUser_page_info")
    @ApiOperation("查询订单分页信息")
    public ResultPageVo<Order>querySysUserPageInfo(){
        return orderCenter.getPageInfo();
    }

    @GetMapping("query_order/{id}")
    @ApiOperation("根据订单id查询订单")
    public ResultVo<Order> queryOrderById(@PathVariable @ApiParam("查询id") long id){
        Order order = orderCenter.queryOrder(id);
        if (order !=null) {
            return ResultUtils.success("查询成功",order);
        }
        return ResultUtils.error("错误");
    }

    @GetMapping("query_order_by_clientId/{orderId}")
    @ApiOperation("根据客户id查询订单")
    public ResultVo<List<Order>>queryOrderByClientId(@PathVariable long id){
        Client byId = orderCenter.getClientService().getById(id);
        String name=null;
        if(byId==null)
            throw new RuntimeException("不存在该用户");
        else
            name=byId.getName();
        List<Order> list = orderCenter.getOrderService().list(new LambdaQueryWrapper<Order>().eq(Order::getReceiver, name));
        return ResultUtils.success("查询成功",list);
    }
//    --------------------------------------------------------------- 上面为查询模块
    @GetMapping("delete_one_order/{id}")
    @ApiOperation("通过id删除订单")
    public ResultVo deleteOneOrder(@PathVariable @ApiParam("通过id订单删除") @NotNull long id){
        if(orderCenter.deleteOrder(id))  return ResultUtils.success("删除成功");
        else return ResultUtils.error();
    }

    @PostMapping("update_one_order")
    @ApiOperation("通过id修改订单")
    public ResultVo updateOrder(@ApiParam("通过Order中的id修改")@RequestBody Order order){
        if(orderCenter.updateOrder(order)) return ResultUtils.success("修改成功");
        else return  ResultUtils.error();
    }
    @PostMapping("/add_order")
    @Transactional
    public ResultVo addOrder( @ApiParam("订单信息") @RequestBody OrderInfo orderInfo,BindingResult bindingResult)
    {
        List<OrderBO> orders = orderInfo.getOrders();
        Order order = orderCenter.addOrder(orders, orderInfo);
        return ResultUtils.success("增加订单成功订单数据为",order);
    }

    @GetMapping("query_mission_by_id/{id}")
    @ApiOperation("通过id查询商品签收单")
    public ResultVo<MissionList>queryMissionListById(@PathVariable long id){
        MissionList missionList = orderCenter.queryMissionById(id);
        return ResultUtils.success("增加订单成功订单数据为",missionList);
    }
    @GetMapping("query_commodity")
    @ApiOperation("查询所有商品信息和分页信息")
    public ResultPageVo<Commodity> queryCommdityPagInfo(){
        return orderCenter.queryCommodityPageInfo();
    }
    @GetMapping("query_commodity_by_id/{id}")
    @ApiOperation("通过id查询商品")
    public ResultVo<Commodity> queryCommdityById(@PathVariable long id){
        return ResultUtils.success("查询成功",orderCenter.queryCommodityById(id));
    }
    @ApiOperation("通过id撤销订单输入id和操作人")
    @GetMapping("logic_delete/{id}/{operator}/{remark}")
    public ResultVo updateCommodityById(@NotNull @PathVariable @ApiParam("订单id") long id,@NotNull @ApiParam("操作人") @PathVariable String operator,@PathVariable  String remark){
        return orderCenter.updateCommodityById(id,operator,remark)?ResultUtils.success("更新成功"):ResultUtils.error();
    }
}
